package packageGenerateData;

import packageInputData.DbConnection;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

public class MainClass {
	
	public static int session=1;
	/**
	 * function 
	 * to read description from console, end reading when input is "-q"
	 * @return			String
	*/
	public static String  getDescription()
	{
		String desc="";
		String exitSt="";
		int i=1;
		try {
			while (!(exitSt.equals("-q")))
			{
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            System.out.println("Enter Description line "+i+", type -q when finish:");
            String s = br.readLine();
            exitSt=s;
            if (!(exitSt.equals("-q")))
	            {
	            	desc=desc+s+"\n";
	            }
            i++;
			}
 
        } catch (IOException e) {
            e.printStackTrace();
        }
		return desc;
	}
	/**
	 * function 
	 * to end  session, and insert end timestamp, into database table "session"
	 * @return			void
	*/
	public static void endSession()
	{
		
		String req="";
		int inserts=0;
		
		//get the session timestamp
		long time = System.currentTimeMillis();
        Timestamp timeDate=new Timestamp(time);        
        //connect to the database
        Connection conn=DbConnection.connectDB();					  			
	  	QueryRunner qRunner = new QueryRunner();
	  	//insert into session table
	  	req="update  session" +
  				" set end_time = ?" +				
  				" where id = ?";
	  	try {
			inserts = qRunner.update(conn,req,
					timeDate,
					MainClass.session);
	  	} catch (SQLException e) {									
			e.printStackTrace();
	  	}
	  	if (inserts==1)
		{
	  		System.out.println("End Session with sucess ");
			inserts=0;
		}
	  	//close connection
		DbUtils.closeQuietly(conn);
		
		
		
	}
	/**
	 * function 	 * 
	 * to start new session, and insert description and timestamp, into database table "session"
	 * @return			void
	 * 
	*/
	public static void startSession()
	{
		
		String desc=getDescription();
		String req="";
		int inserts=0;
		
		//get the session timestamp
		long time = System.currentTimeMillis();
        Timestamp timeDate=new Timestamp(time);        
        //connect to the database
        Connection conn=DbConnection.connectDB();					  			
	  	QueryRunner qRunner = new QueryRunner();
	  	//insert into session table
	  	req="INSERT INTO session" +
  				"(description,start_time)" +				
  				"VALUES (?,?)";
	  	try {
			inserts = qRunner.update(conn,req,
					desc,
					timeDate);
	  	} catch (SQLException e) {									
			e.printStackTrace();
	  	}
	  	if (inserts==1)
		{
	  		System.out.println("Insertion in session done ");
			inserts=0;
		}
	  	//close connection
		DbUtils.closeQuietly(conn);
		
		MainClass.session=(Integer)DbConnection.getFromDB("id","session");
	}
	/**
	 * function 
	 * to generate all needed data in the right order
	 * @return			void
	*/
	public static void startGenerateData()
	{
		
		MainClass.startSession();
		System.out.println("Calculation in process\t STEP 1: generateEpok");
		GenerateDataSPOK.generateEpok();
		System.out.println("Calculation in process\t STEP 2: generateVcopsepok");
		GenerateDataSPOK.generateVcopsepok();
		System.out.println("Calculation in process\t STEP 3: generatePTU");
		GenerateDataPTU.generatePTU();
		System.out.println("Calculation in process\t STEP 4: generatePTU2");
		GenerateDataPTU.generatePTU2();
		System.out.println("Calculation in process\t STEP 5: generateZTDTemp");
		GenerateDataZTD.generateZTDTemp();
		System.out.println("Calculation in process\t STEP 6: generateZTDHmf");
		GenerateDataZTD.generateZTDHmf();
		System.out.println("Calculation in process\t STEP 7: generateZHD");
		GenerateDataZHD.generateZHD();
		System.out.println("Calculation in process\t STEP 8: generateZWD");
		GenerateDataZWD.generateZWD();
		System.out.println("Calculation in process\t STEP 9: generateIWV");
		GenerateDataIWV.generateIWV();
		System.out.println("Calculation in process\t STEP 10: generateGraphIWV");
		GenerateDataIWV.generateGraphIWV();
		System.out.println("Calculation in process\t STEP 11: generateSIWV");
		GenerateDataIWV.generateSIWV();
		System.out.println("Calculation in process\t STEP 12: generateSXYZ");
		GenerateDataSXYZ.generateSXYZ();
		System.out.println("Calculation in process\t STEP 13: generateTomo");
		GenerateDataTOMO.generateTomo();
		System.out.println("Calculation in process\t STEP 14: generateGrilleData");
		GenerateDataGrille.generateGrilleData();
		System.out.println("Calculation in process\t STEP 15: generateInterpIWV");
		GenerateDataIWV.generateInterpIWV();
		System.out.println("Calculation in process\t STEP 16: generateRaysTomo");
		GenerateDataTOMO.generateRaysTomo();
	}
	
	public static void main (String args[])
	{
	  	
				DbConnection.getConnexionParam();
				
				//get the session timestamp
				long time = System.currentTimeMillis();
		        Timestamp timeDate=new Timestamp(time);
		        
		        		startGenerateData();
		        		
		        System.out.println("Copying data to Log...");
		        DbConnection.copyToLog();
		        System.out.println("deleting unwanted data");
		        DbConnection.clearGeneratedData();
		        		
		        //get the session timestamp
				time = System.currentTimeMillis();
		        Timestamp timeDate1=new Timestamp(time);
		        
		        System.out.println("\n\n\n"+timeDate+"\n"+timeDate1);
		        
						endSession();
		        
		      
	}


}
